<template>
    <el-card style="height:790px" class="nav_card">
        <div slot="header" style="font-weight:bold;font-size:15px;" >
            {{grabSource.station_id}} - {{grabSource.station_name_cn}} - {{grabSource.entry_url}}
        </div>
        <el-form ref="grabSourceForm" label-position="left" label-width="150px" style="height: 710px;overflow-y: scroll" v-loading="formLoading" element-loading-text="数据加载中...">
            <el-form-item label="站点中文名称">
                <el-input v-model="grabSource.station_name_cn" style="width:1000px;"></el-input>
            </el-form-item>
            <el-form-item label="入口url">
                <el-input v-model="grabSource.entry_url" style="width:1000px;"></el-input>
                <span class="alert"><i class="el-icon-info"></i> 种子</span>
            </el-form-item>
            <el-form-item label="数据源">
                <el-select v-model="grabSource.data_source_id" style="width:300px;" placeholder="请选择" filterable>
                    <el-option v-for="item in dataSourceList" :label="item.data_source_name" :value="item.data_source_id" :key="item.data_source_id" >
                    </el-option>
                </el-select>
                <span class="alert"><i class="el-icon-info"></i> 输入关键字可搜索选项</span>
            </el-form-item>
            <el-form-item label="资讯类型">
                <el-select v-model="grabSource.info_type_id" style="width:300px;" placeholder="请选择" filterable>
                    <el-option v-for="item in infoTypeList" :label="item.info_type_name" :value="item.info_type_id" :key="item.info_type_id" >
                    </el-option>
                </el-select>
                <span class="alert"><i class="el-icon-info"></i> 输入关键字可搜索选项</span>
            </el-form-item>
            <el-form-item label="分类">
                <el-select v-model="grabSource.category_id" style="width:500px;" placeholder="请选择" filterable>
                    <el-option v-for="item in categoryList" :label="`${item.category_id}--${item.category_name}`" :value="item.category_id" :key="item.category_id" >
                        <span style="float: left">{{ item.category_id }}--{{ item.category_name }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">层级：{{ item.category_level }}</span>
                    </el-option>
                </el-select>
                <span class="alert"><i class="el-icon-info"></i> 输入关键字可搜索选项</span>
            </el-form-item>
            <el-form-item label="抓取频率">
                <el-input v-model="grabSource.grab_freq" style="width:500px;" ></el-input>
                <span class="alert"><i class="el-icon-info"></i> 单位：秒，表示多长时间往爬虫里丢一次种子</span>
            </el-form-item>
            <el-form-item label="是否解析附件">
                <el-switch v-model="grabSource.is_parser_attachment" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否清洗">
                <el-switch v-model="grabSource.is_norm_handle" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否分词">
                <el-switch v-model="grabSource.is_segment" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否自动标签">
                <el-switch v-model="grabSource.is_auto_labeling" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否去重">
                <el-switch v-model="grabSource.is_remove_duplication" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否自动摘要">
                <el-switch v-model="grabSource.is_autoabstract" :active-value = "1" :inactive-value = "0"></el-switch>
            </el-form-item>
            <el-form-item label="是否使用新爬虫系统">
                <el-switch v-model="grabSource.crawler_type" :active-value = "2" :inactive-value = "1"></el-switch>
            </el-form-item>
            <!--
            <el-form-item label="规则列表">
                <el-input v-model="grabSource.config_list" style="width:500px;" ></el-input>(抓取到的url根据规则列表传递直到详情页结束，列表页一定是以详情页结束，多少层列表页无限定，或者也可以只有一个详情页)
            </el-form-item>
            -->
            <el-form-item label="业务详情">
                <el-select v-model="businessDetailsSelectedList" multiple placeholder="请选择" style="width:400px;" filterable>
                    <el-option
                    v-for="item in businessDetailsList"
                    :key="item.businessId"
                    :label="`${item.businessId}--${item.busName}`"
                    :value="`${item.businessId}`">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="规则列表">
                <el-select v-model="ruleSelectedList" style="width:600px;" multiple placeholder="请选择" filterable>
                    <el-option
                    v-for="item in ruleList"
                    :key="item.config_id"
                    :label="`${item.config_id}--${item.desc}`"
                    :value="`${item.config_id}`">
                    </el-option>
                </el-select>
                <span class="alert"><i class="el-icon-info"></i> 抓取到的url根据规则列表传递直到详情页结束，列表页一定是以详情页结束，多少层列表页无限定，或者也可以只有一个详情页</span>
            </el-form-item>
            <el-form-item label="调度主队列">
                <el-select v-model="grabSource.main_list_name" placeholder="请选择" filterable allow-create>
                    <el-option
                    v-for="mainListName in mainListNameList"
                    :key="mainListName"
                    :label="mainListName"
                    :value="mainListName">
                    </el-option>
                </el-select>
                <span class="alert"><i class="el-icon-info"></i> 也可直接输入新的队列名称</span>
            </el-form-item>
            <el-form-item label="抓取时间限制">
                <el-input v-model="grabSource.grab_timer" style="width:500px;" ></el-input>
                <span class="alert"><i class="el-icon-info"></i> 抓取时间限制在抓取频率检测周期的前提下设定生效的区间，如9|10 含义是只有在9点到10点间才丢种子</span>
            </el-form-item>
            <el-form-item label="站点优先级">
                <el-input-number v-model="grabSource.station_degree" style="width:150px;" :min = "0"></el-input-number>
                <span class="alert"><i class="el-icon-info"></i> 供资讯接口使用</span>
            </el-form-item>
            <el-form-item label="是否激活">
                <el-switch v-model="grabSource.status" :active-value = "1" :inactive-value = "0"></el-switch>
                <span class="alert"><i class="el-icon-info"></i> 关闭后将不会抓取</span>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submitForm">保存</el-button>
                <!--<el-button>取消</el-button>-->
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import { getGrabSourceById, getGrabSourceList, saveGrabSource, getDataSourceList, getInfoTypeList, getCategoryList, getRuleList, getBusinessDetailsList } from '../../api/crawl.js';
import lodash from 'lodash';
export default {
    data(){
        return {
            grabSourceForm : {},
            formLoading : false,
            submitLoading : false,
            dataSourceList : [],
            infoTypeList: [],
            categoryList : [],
            ruleList : [],
            businessDetailsList : [],
            grabSource : {
                station_id : 0,
                station_name_cn : '',
                entry_url : '',
                data_source_id : '',
                grab_freq : '',
                info_type_id : '',
                category_id : '',
                is_parser_attachment : 0,
                is_norm_handle : 1,
                is_segment : 1,
                is_auto_labeling : 1,
                is_remove_duplication : 1,
                is_autoabstract : 0,
                status : 0,
                crawler_type : 2,
                config_list : '',
                main_list_name : 'news_queue_all',
                grab_timer : '',
                station_degree : 0,
            },
            ruleSelectedList : [],//规则列表-多选下拉框集合
            businessDetailsSelectedList : [],//业务详情列表-多选下拉框集合
            mainListNameList : [],//调度主队列列表
        }
    },
    components: {
    },
    created(){
        this.initRuleList();
        const station_id = this.$route.params.station_id;
        if(Number(station_id)){
            this.grabSource.station_id = station_id;
            this.initGrabSourceById();
        }else{
            this.grabSource.station_id = 0;
        }
        this.initDataSourceList();
        this.initCategoryList();
        this.initInfoTypeList();
        this.initMainListNameList();
        this.initBusinessDetailsList();
    },
    watch: {
    },
    methods: {
        initDataSourceList(){
            getDataSourceList().then(data=>{
                this.dataSourceList = data.rows;
            })
        },
        initCategoryList(){
            getCategoryList().then(data=>{
                this.categoryList = data.rows;
                
            })
        },
        initInfoTypeList(){
            getInfoTypeList().then(data=>{
                this.infoTypeList = data.rows;
                this.infoTypeList = this.infoTypeList.filter(item => Number(item.status) === 1 );
            })
        },
        initMainListNameList(){
            getGrabSourceList().then(data=>{
                let grabSourceList = data.rows;
                grabSourceList.forEach(item=>{
                    let mainListName = item.main_list_name ? item.main_list_name.trim() : '';
                    if(mainListName && !this.mainListNameList.includes(mainListName)){
                        this.mainListNameList.push(mainListName);
                    }
                })
            })
        },
        initBusinessDetailsList(){
            getBusinessDetailsList().then(data=>{
                this.businessDetailsList = data.rows;
                this.businessDetailsList = this.businessDetailsList.filter(item => Number(item.isValid) === 1 );
            }).catch(e=>{
                console.log(e);
            });
        },
        async initRuleList(){
            const ruleData = await getRuleList();
            this.ruleList = ruleData.rows;
        },
        async initGrabSourceById(){
            this.formLoading = true;
            const data = await getGrabSourceById(this.grabSource.station_id);
            console.log('getGrabSourceById------------', data);
            this.grabSource = data.rows[0];
            if(!this.ruleList.length){
                const ruleData = await getRuleList();
                this.ruleList = ruleData.rows;
            }
            //加载规则列表下拉框
            if(this.grabSource.businessId){
                this.businessDetailsSelectedList = this.grabSource.businessId.split(',');
            }

            //加载业务详情列表下拉框
            if(this.grabSource.config_list){
                this.ruleSelectedList = this.grabSource.config_list.split(',');
            }
            this.formLoading = false;
        },
        async submitForm(){
            try{
                /*保存数据*/
                this.grabSource.config_list = this.ruleSelectedList.join(',');
                this.grabSource.businessId = this.businessDetailsSelectedList.join(',');
                const postData = Object.assign({}, this.grabSource);
                this.submitLoading = true;
                const res = await saveGrabSource(postData);
                this.submitLoading = false;
                console.log('saveGrabSource------',res)
                if(res){
                    if (res.station_id) {
                        this.$alert('新增成功！', '提示', {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.$router.push('/grabSourceList');
                            }
                        });
                    }else if (res === 1) {
                        this.$confirm('修改成功！', '提示', {
                            confirmButtonText: '返回列表页',
                            cancelButtonText: '留在本页',
                            type: 'warning'
                        }).then(() => {
                            this.$router.push('/grabSourceList');
                        }).catch((e) => {
                            //console.error(e);
                        });
                    }
                }else{
                    this.$message({
                        message: '保存失败！',
                        type: 'danger'
                    });
                }
            }catch(e){
                console.error(e);
            }
        }
    }
};
</script>